﻿namespace CloudNinja.Metering
{
    using System;
    using System.Configuration;

    using CloudNinja.Metering.Common.Configuration;

    using Microsoft.WindowsAzure.Storage;

    public class Settings
    {
        public const string LeasesContainerName = "leases";

        public const string LeaseBlobName = "meteringlease";

        public const double LeaseRenewPeriod = 30;


        public static CloudStorageAccount AzureStorageAccount
        {
            get
            {
                var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

                var meteringSectionObj = config.GetSection("MeteringConfiguration");

                var meteringSection = meteringSectionObj as MeteringSection;

                if (meteringSection == null)
                {
                    throw new ConfigurationErrorsException("Cannot find MeteringConfiguration section in the .config file.");
                }

                var blockConnectionString = meteringSection.CommonSettings.BlockConnectionString;

                try
                {
                    return CloudStorageAccount.Parse(blockConnectionString);
                }
                catch (Exception)
                {
                    throw new ApplicationException("Cannot establish storage connection with the given BlockConnectionString.");
                }
            }
        }
    }
}
